home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / extend5.zip / SHRINK.PAS < prev   
Pascal/Delphi Source File  |  1990-10-19  |  5KB  |  130 lines

  1. unit Shrink;
  2.  
  3. { This unit allows you to allocate memory from the DOS memory pool rather than
  4.   from the Turbo Pascal heap.  It also provides a procedure for shrinking the
  5.   current program to free up DOS memory.
  6.  
  7.   Scott Bussinger
  8.   Professional Practice Systems
  9.   110 South 131st Street
  10.   Tacoma, WA  98444
  11.   (206)531-8944
  12.   Compuserve [72247,2671] }
  13.  
  14. { ** Revision History **
  15.   1 SHRINK.PAS 15-Sep-89,`SCOTT' Initial version of SHRINK unit
  16.   2 SHRINK.PAS 19-Oct-90,`SCOTT'
  17.            Added support for Turbo Pascal 6's new heap manager
  18.   ** Revision History ** }
  19.  
  20. interface
  21.  
  22. procedure DosNew(var P: pointer;
  23.                      Bytes: word);
  24.   { Get a pointer to a chunk of memory from DOS.  Returns NIL if
  25.     sufficient DOS memory is not available. }
  26.  
  27. procedure DosDispose(var P: pointer);
  28.   { Return an allocated chunk of memory to DOS.  Only call this function
  29.     with pointers allocated with DosNew or DosNewShrink. }
  30.  
  31. procedure DosNewShrink(var P: pointer;
  32.                            Bytes: word);
  33.   { Get a pointer to a chunk of memory from DOS, shrinking current program
  34.     to gain DOS memory if necessary.  Returns NIL if sufficient DOS memory
  35.     is not available and there is insufficient free space in the heap to
  36.     allow program to be shrunk to accomodate the request. }
  37.  
  38. implementation
  39.  
  40. uses Dos;
  41.  
  42. const DosOverhead = 1;                           { Extra number of paragraphs that DOS requires in overhead for MCB chain }
  43.  
  44. function Linear(P: pointer): longint;
  45.   { Return the pointer as a linear longint value }
  46.   begin
  47.   Linear := (longint(seg(P^)) shl 4) + ofs(P^)
  48.   end;
  49.  
  50. procedure DosNew(var P: pointer;
  51.                      Bytes: word);
  52.   { Get a pointer to a chunk of memory from DOS.  Returns NIL if
  53.     sufficient DOS memory is not available. }
  54.   var SegsToAllocate: word;
  55.       Regs: Registers;
  56.   begin
  57.   SegsToAllocate := (Bytes+15) shr 4;            { DOS allocates memory in paragraph sized pieces only }
  58.   with Regs do
  59.     begin
  60.     AH := $48;
  61.     BX := SegsToAllocate;
  62.     MsDos(Regs);
  63.     if odd(Flags)
  64.      then
  65.       P := nil                                   { No memory available }
  66.      else
  67.       P := ptr(AX,$0000)                         { Return pointer to memory block }
  68.     end
  69.   end;
  70.  
  71. procedure DosDispose(var P: pointer);
  72.   { Return an allocated chunk of memory to DOS.  Only call this function
  73.     with pointers allocated with DosNew or DosNewShrink. }
  74.   var Regs: Registers;
  75.   begin
  76.   with Regs do
  77.     begin
  78.     AH := $49;
  79.     ES := seg(P^);
  80.     MsDos(Regs)
  81.     end
  82.   end;
  83.  
  84. procedure DosNewShrink(var P: pointer;
  85.                            Bytes: word);
  86.   { Get a pointer to a chunk of memory from DOS, shrinking current program
  87.     to gain DOS memory if necessary.  Returns NIL if sufficient DOS memory
  88.     is not available and there is insufficient free space in the heap to
  89.     allow program to be shrunk to accomodate the request. }
  90.   var BytesToAllocate: word;
  91.       Regs: Registers;
  92.   begin
  93.   BytesToAllocate := (((Bytes+15) shr 4) + DosOverhead) shl 4;
  94.   DosNew(P,Bytes);                               { Try to get memory the easy way first }
  95.   {$IFDEF VER60}                                 { Check for Turbo 6's new heap manager }
  96.   if (P=nil) and (Linear(HeapEnd)-Linear(HeapPtr)>=BytesToAllocate) then
  97.     begin                                        { The easy method didn't work but there is sufficient space in the heap }
  98.     dec(longint(HeapEnd),longint(BytesToAllocate) shl 12); { Move the top of the heap down }
  99.     with Regs do
  100.       begin
  101.       AH := $4A;
  102.       BX := seg(HeapEnd^) - prefixseg - (BytesToAllocate shr 4);
  103.       ES := prefixseg;
  104.       MsDos(Regs)
  105.       end;
  106.     DosNew(P,Bytes)                              { Try the DOS allocation one more time }
  107.     end
  108.   {$ELSE}
  109.   if (P=nil) and                                 { Handle the old free list style heap }
  110.      (((ofs(FreePtr^)=0) and (Linear(FreePtr)+$10000-Linear(HeapPtr)>=BytesToAllocate)) or
  111.       ((ofs(FreePtr^)<>0) and (Linear(FreePtr)-Linear(HeapPtr)>=BytesToAllocate))) then
  112.     begin                                        { The easy method didn't work but there is sufficient space in the heap }
  113.     OldFreePtr := FreePtr;
  114.     dec(longint(FreePtr),longint(BytesToAllocate) shl 12); { Decrement the segment of the pointer to the free list }
  115.     if ofs(OldFreePtr^) <> 0 then                { If free list is empty, then there's nothing to move }
  116.       move(OldFreePtr^,FreePtr^,$10000-ofs(OldFreePtr^)); { Otherwise, move the free list down in memory }
  117.     with Regs do
  118.       begin
  119.       AH := $4A;
  120.       BX := seg(OldFreePtr^) + $1000 - prefixseg - (BytesToAllocate shr 4);
  121.       ES := prefixseg;
  122.       MsDos(Regs)
  123.       end;
  124.     DosNew(P,Bytes)                              { Try the DOS allocation one more time }
  125.     end
  126.   {$ENDIF}
  127.   end;
  128.  
  129. end.
  130.